<template>
  <div class="innerContainer">
    <common-title>
      <template #expand>
        <el-button type="primary" @click="onAdd">新增设备</el-button>

        <el-button plain @click="openImport(1)">导入设备</el-button>

        <el-button type="success" @click="onSelectProject">关联项目</el-button>

        <el-button @click="handleFlushedCache">刷新缓存</el-button>
      </template>
    </common-title>
    <div class="innerPane">
      <div class="filterArea">
        <el-form :inline="true" :model="filterForm" class="demo-form-inline">
          <el-form-item label="设备名称">
            <el-input @keyup.enter="search" v-model="searchForm.name" placeholder="请输入设备名称"></el-input>
          </el-form-item>
          <el-form-item label="设备编码">
            <el-input @keyup.enter="search" v-model="searchForm.code" placeholder="请输入设备编码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button plain type="primary" @click="search">查询</el-button>
            <el-button plain @click="onClear">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <el-table ref="tableRef" :data="tableData" border row-key="mapMacCode" style="width: 100%; margin-top: 20px"
        :cell-style="{ textAlign: 'center' }">
        <el-table-column header-align="center" type="selection" width="50" />
        <el-table-column header-align="center" type="index" label="序号" width="50" :index="indexMethod" />
        <el-table-column header-align="center" prop="mapName" label="设备名"  />
        <el-table-column header-align="center" prop="modelCode" label="产品型号" width="180" />

        <el-table-column header-align="center" prop="mapSnCode" label="sn码" width="160" />
        <el-table-column header-align="center" prop="mapMacCode" label="mac地址" width="160" />
        <el-table-column header-align="center" prop="manufactureDate" label="出厂时间" width="180" />
        <el-table-column header-align="center" prop="img" label="设备图片" width="140">
          <template #default="scope">
            <el-image v-if="scope.row.img" :src="scope.row.img" style="object-fit: cover; cursor: pointer;"
              :preview-src-list="[scope.row.img]" :initial-index="0" fit="cover" preview-teleported />
          </template>
        </el-table-column>
        <el-table-column header-align="center" prop="projectName" label="所属项目" width="160" />
        <el-table-column header-align="center" prop="lastRcvTime" label="近期通信时间" width="180" />

        <el-table-column label="操作" header-align="center" width="180" prop="col9">
          <template #default="scope">
            <el-link plain type="primary" size="small" @click="onEdit(scope.row)">编辑</el-link>

            <el-link plain type="danger" size="small" @click="onDel(scope.row)">删除</el-link> 
          </template>
        </el-table-column>

      </el-table>
      <!-- 分页 -->
      <el-pagination class="pagination" :current-page="searchForm.current" :page-size="searchForm.size"
        :page-sizes="[10, 20, 30, 40]" layout="->,total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
    <!-- 查看弹出层 -->
    <detail-view ref="viewObj" @confirm="search"></detail-view>
    <!-- 导入弹出层 -->
    <import-dialog ref="importDialog" @confirm="search"></import-dialog>


    <select-project ref="selectProject" @confirm="search" />

    <serial-setting ref="serialSetting" />
  </div>


</template>

<script setup>
import CommonTitle from "@/components/CommonTitle.vue";
import ImportDialog from "@/components/ImportDialog.vue";

import SelectProject from "./SelectProject.vue";
import SerialSetting from "./serialSetting.vue";

import { onMounted, reactive, ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";

import {
  getHardwareInfo,
  hardwareDel,
  getProductList, baseFlushedCache
} from "@/utils/requests";
import DetailView from "./view.vue";

const router = useRouter();

const tableRef = ref();

let modelList = ref([]);

// Dom 挂载之后
onMounted(async () => {
  modelList.value = await getProductList();
  search();
});

// 用户数据
let tableData = ref([]);

let total = ref(0);

const importDialog = ref();

/**
 * 导入
 */
const openImport = (code) => {
  importDialog.value.openDialog({ code, fileName: "硬件信息导入模板.xlsx" });
};

const handleFlushedCache = () => {
  baseFlushedCache().then(async () => {
    ElMessage.success("刷新成功 ");
  })
    .catch(() => {
      ElMessage({
        type: "wran",
        message: "刷新异常",
      });
    });
}


// 搜索条件
const searchForm = reactive({
  current: 1,
  size: 10,
  name: null,
  code: null,

  messageType: 1,
});

const indexMethod = (index) => {
  return (searchForm.current - 1) * searchForm.size + index + 1;
};

const onClear = () => {
  searchForm.name = "";
  searchForm.code = "";

  search();
};

const handleSizeChange = (size) => {
  searchForm.size = size;
  searchForm.current = 1;
  search();
};
const handleCurrentChange = (current) => {
  searchForm.current = current;
  search();
};

const search = () => {
  getHardwareInfo(searchForm).then((res) => {
    tableData.value = res.records;
    total.value = res.total;
  });
};

const selectProject = ref();
const onSelectProject = () => {
  selectProject.value.openDialog(tableRef.value?.getSelectionRows());
};

const serialSetting = ref();
const handleSerialSetting =(row)=>{
   serialSetting.value.openDialog(row); 
}

// 详情查看弹窗
const viewObj = ref();

//添加
const onAdd = () => {
  viewObj.value.openDialog(modelList.value);
};

//编辑
const onEdit = (row) => {
  let rowData = Object.assign({ _o: "w" }, row);
  viewObj.value.openDialog(modelList.value, rowData);
};

const onDel = (rowData) => {
  ElMessageBox.confirm("确定要删除该设备信息吗?", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      const res = await hardwareDel({ ids: rowData.id });

      ElMessage.success("删除成功");
      searchForm.current = 1;
      search();
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除",
      });
    });
};

</script>

<style scoped>
.filterArea {
  display: flex;
  justify-content: space-between;
  padding-top: 10px;
}

.filterArea .el-select {
  width: 120px;
}

.el-table .el-link {
  margin-right: 10px;
}
</style>
